{% extends "base.html" %}

{% block title %}用户管理 - 墨韵图书共享平台{% endblock %}

{% block content %}
<div class="admin-panel">
    <div class="row mb-4">
        <div class="col-md-12">
            <h2 class="ink-decoration mb-4">用户管理</h2>
            <p class="text-muted">在这里您可以管理所有用户，包括设置管理员权限等。</p>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-3">
            <div class="card ink-card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">管理菜单</h5>
                </div>
                <div class="list-group list-group-flush">
                    <a href="{{ url_for('admin_panel') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-tachometer-alt me-2"></i> 仪表盘
                    </a>
                    <a href="{{ url_for('admin_books') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-book me-2"></i> 图书管理
                    </a>
                    <a href="{{ url_for('admin_users') }}" class="list-group-item list-group-item-action active">
                        <i class="fas fa-users me-2"></i> 用户管理
                    </a>
                    <a href="{{ url_for('admin_categories') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-tags me-2"></i> 分类管理
                    </a>
                    <a href="{{ url_for('admin_statistics') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-chart-bar me-2"></i> 使用统计
                    </a>
                </div>
            </div>
        </div>
        
        <div class="col-md-9">
            <div class="card ink-card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">用户列表</h5>
                    <span class="badge bg-info">{{ users.total }} 个用户</span>
                </div>
                <div class="card-body">
                    {% if users.items %}
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>头像</th>
                                        <th>用户名</th>
                                        <th>学号</th>
                                        <th>类型</th>
                                        <th>注册时间</th>
                                        <th>最后活动</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for user in users.items %}
                                        <tr>
                                            <td>{{ user.id }}</td>
                                            <td>
                                                <img src="{{ url_for('static', filename='avatars/' + user.avatar) }}" 
                                                     class="user-avatar" alt="{{ user.username }}">
                                            </td>
                                            <td>
                                                {{ user.username }}
                                                {% if user.is_admin %}
                                                    <span class="admin-badge">管理员</span>
                                                {% endif %}
                                            </td>
                                            <td>{{ user.student_id }}</td>
                                            <td>{% if user.is_teacher %}教师{% else %}学生{% endif %}</td>
                                            <td>{{ user.created_at.strftime('%Y-%m-%d') }}</td>
                                            <td>
                                                {% if user.last_seen %}
                                                    {{ user.last_seen.strftime('%Y-%m-%d %H:%M') }}
                                                {% else %}
                                                    未记录
                                                {% endif %}
                                            </td>
                                            <td>
                                                <div class="btn-group btn-group-sm">
                                                    {% if user.id != current_user.id %}
                                                        <button type="button" class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#adminModal{{ user.id }}">
                                                            {% if user.is_admin %}
                                                                <i class="fas fa-user-minus"></i>
                                                            {% else %}
                                                                <i class="fas fa-user-shield"></i>
                                                            {% endif %}
                                                        </button>
                                                    {% endif %}
                                                </div>
                                                
                                                <!-- 管理员权限模态框 -->
                                                <div class="modal fade" id="adminModal{{ user.id }}" tabindex="-1" aria-hidden="true">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title">{% if user.is_admin %}移除{% else %}授予{% endif %}管理员权限</h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                            </div>
                                                            <div class="modal-body">
                                                                <p>
                                                                    您确定要{% if user.is_admin %}移除{% else %}授予{% endif %}用户 {{ user.username }} 的管理员权限吗？
                                                                </p>
                                                                {% if user.is_admin %}
                                                                    <div class="alert alert-warning">
                                                                        <i class="fas fa-exclamation-triangle me-2"></i>
                                                                        移除后，该用户将失去所有管理功能的访问权限。
                                                                    </div>
                                                                {% else %}
                                                                    <div class="alert alert-warning">
                                                                        <i class="fas fa-exclamation-triangle me-2"></i>
                                                                        授予后，该用户将拥有对平台的全部管理权限。
                                                                    </div>
                                                                {% endif %}
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                                                <form action="{{ url_for('toggle_admin', user_id=user.id) }}" method="post">
                                                                    <button type="submit" class="btn {% if user.is_admin %}btn-danger{% else %}btn-primary{% endif %}">
                                                                        {% if user.is_admin %}移除权限{% else %}授予权限{% endif %}
                                                                    </button>
                                                                </form>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        
                        <!-- 分页 -->
                        {% if users.pages > 1 %}
                            <nav aria-label="用户分页">
                                <ul class="pagination justify-content-center">
                                    {% if users.has_prev %}
                                        <li class="page-item">
                                            <a class="page-link" href="{{ url_for('admin_users', page=users.prev_num) }}">上一页</a>
                                        </li>
                                    {% else %}
                                        <li class="page-item disabled">
                                            <a class="page-link" href="#" tabindex="-1" aria-disabled="true">上一页</a>
                                        </li>
                                    {% endif %}
                                    
                                    {% for page_num in users.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=2) %}
                                        {% if page_num %}
                                            {% if page_num == users.page %}
                                                <li class="page-item active">
                                                    <a class="page-link" href="#">{{ page_num }}</a>
                                                </li>
                                            {% else %}
                                                <li class="page-item">
                                                    <a class="page-link" href="{{ url_for('admin_users', page=page_num) }}">{{ page_num }}</a>
                                                </li>
                                            {% endif %}
                                        {% else %}
                                            <li class="page-item disabled">
                                                <a class="page-link" href="#">...</a>
                                            </li>
                                        {% endif %}
                                    {% endfor %}
                                    
                                    {% if users.has_next %}
                                        <li class="page-item">
                                            <a class="page-link" href="{{ url_for('admin_users', page=users.next_num) }}">下一页</a>
                                        </li>
                                    {% else %}
                                        <li class="page-item disabled">
                                            <a class="page-link" href="#" tabindex="-1" aria-disabled="true">下一页</a>
                                        </li>
                                    {% endif %}
                                </ul>
                            </nav>
                        {% endif %}
                    {% else %}
                        <p class="text-center text-muted">暂无用户</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %} 